﻿using MN.Framework.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;

namespace MN.Framework.UI.Win.Forms
{
    public partial class CommandForm : MnBaseForm
    {
        private Telerik.WinControls.UI.RadDropDownList ddlCommandType;
        private Telerik.WinControls.UI.RadButton btnExecute;
        private Telerik.WinControls.UI.RadLabel lblCommandTitle;
        private Telerik.WinControls.UI.RadButton btnClear;
        private Telerik.WinControls.UI.RadTextBox txtCommand;

        public CommandForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            Telerik.WinControls.UI.RadListDataItem radListDataItem2 = new Telerik.WinControls.UI.RadListDataItem();
            this.ddlCommandType = new Telerik.WinControls.UI.RadDropDownList();
            this.btnExecute = new Telerik.WinControls.UI.RadButton();
            this.lblCommandTitle = new Telerik.WinControls.UI.RadLabel();
            this.txtCommand = new Telerik.WinControls.UI.RadTextBox();
            this.btnClear = new Telerik.WinControls.UI.RadButton();
            ((System.ComponentModel.ISupportInitialize)(this.ddlCommandType)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.btnExecute)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.lblCommandTitle)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtCommand)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.btnClear)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // ddlCommandType
            // 
            radListDataItem2.Selected = true;
            radListDataItem2.Tag = "Sql";
            radListDataItem2.Text = global::MN.Framework.UI.Resource.CommandForm.SqlCommand;
            radListDataItem2.TextWrap = true;
            this.ddlCommandType.Items.Add(radListDataItem2);
            this.ddlCommandType.Location = new System.Drawing.Point(30, 252);
            this.ddlCommandType.Name = "ddlCommandType";
            this.ddlCommandType.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.ddlCommandType.Size = new System.Drawing.Size(183, 20);
            this.ddlCommandType.TabIndex = 7;
            this.ddlCommandType.Text = "دستورات بانک اطلاعاتی";
            // 
            // btnExecute
            // 
            this.btnExecute.Location = new System.Drawing.Point(433, 248);
            this.btnExecute.Name = "btnExecute";
            this.btnExecute.Size = new System.Drawing.Size(110, 24);
            this.btnExecute.TabIndex = 6;
            this.btnExecute.Text = global::MN.Framework.UI.Resource.CommandForm.ExecuteCommand;
            this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
            // 
            // lblCommandTitle
            // 
            this.lblCommandTitle.Location = new System.Drawing.Point(378, 13);
            this.lblCommandTitle.Name = "lblCommandTitle";
            this.lblCommandTitle.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.lblCommandTitle.Size = new System.Drawing.Size(163, 18);
            this.lblCommandTitle.TabIndex = 5;
            this.lblCommandTitle.Text = "دستورات را در کادر زیر وارد نمایید:";
            this.lblCommandTitle.TextAlignment = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtCommand
            // 
            this.txtCommand.AutoSize = false;
            this.txtCommand.Location = new System.Drawing.Point(30, 44);
            this.txtCommand.Multiline = true;
            this.txtCommand.Name = "txtCommand";
            this.txtCommand.Size = new System.Drawing.Size(513, 184);
            this.txtCommand.TabIndex = 4;
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(308, 248);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(110, 24);
            this.btnClear.TabIndex = 8;
            this.btnClear.Text = "پاک کردن دستورات";
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // CommandForm
            // 
            this.ClientSize = new System.Drawing.Size(575, 299);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.ddlCommandType);
            this.Controls.Add(this.btnExecute);
            this.Controls.Add(this.lblCommandTitle);
            this.Controls.Add(this.txtCommand);
            this.Name = "CommandForm";
            // 
            // 
            // 
            this.RootElement.ApplyShapeToControl = true;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "فرم اجرای دستورات";
            ((System.ComponentModel.ISupportInitialize)(this.ddlCommandType)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.btnExecute)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.lblCommandTitle)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtCommand)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.btnClear)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            var context = BaseContext.GetContext();
            try
            {

                var commands = txtCommand.Text.ToLower().Split(new string[] { "\ngo" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var command in commands)
                {
                    context.Database.ExecuteSqlCommand(txtCommand.Text);
                }
                RadMessageBox.Show("اجرای دستورات با موفقیت پایان پذیرفت", "دستورات انجام شد", MessageBoxButtons.OK, RadMessageIcon.Info);
            }
            catch (Exception ex)
            {
                RadMessageBox.Show("در اجاری دستور خطایی رخ داد \n" + ex.Message, "خطا در اجرا", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCommand.Text = string.Empty;
        }


    }
}
